/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.serialize;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Array;

/**
 * @author creativor
 * 
 */
final class ArrayType extends SerializableType<Object> {

	/**
	 * @param type
	 */
	protected ArrayType(final byte type) {
		super(type);
	}

	@Override
	Object read(final DataInput dis) throws SerializeFormatException, IOException,
			UnsupportedTypeException {
		final int arrayLength = dis.readInt();
		final byte type = dis.readByte();
		final SerializableType<?> componentType = Types.getType(type);
		return componentType.readArray(dis, arrayLength);
	}

	@Override
	Object readArray(final DataInput dis, final int arrayLength) {
		throw new UnsupportedOperationException();
	}

	@Override
	void write(final DataOutput dos, final Object obj) throws UnsupportedTypeException, IOException {
		final Class<?> componentType = obj.getClass().getComponentType();
		final SerializableType<?> compSType = Types.typeOf(componentType);
		final int arrayLength = Array.getLength(obj);
		dos.writeInt(arrayLength);
		dos.writeByte(compSType.getType());
		compSType.writeArray(dos, obj);
	}

	@Override
	void writeArray(final DataOutput dos, final Object array) {
		throw new UnsupportedOperationException();
	}
}